package com.hnyfkj.jyindustry.biz.sysrole.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.dao.SysMemberInfoDao;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.entity.SysMemberInfoEntity;
import com.hnyfkj.jyindustry.biz.sysrole.dao.SysRoleDao;
import com.hnyfkj.jyindustry.biz.sysrole.dto.SysRoleInsertDto;
import com.hnyfkj.jyindustry.biz.sysrole.dto.SysRoleQueryDto;
import com.hnyfkj.jyindustry.biz.sysrole.dto.SysRoleUpdateDto;
import com.hnyfkj.jyindustry.biz.sysrole.entity.SysRoleEntity;
import com.hnyfkj.jyindustry.biz.sysrole.service.SysRoleService;
import com.hnyfkj.jyindustry.common.exception.HnyfkjBusinessException;
import com.hnyfkj.jyindustry.common.utils.PageUtils;
import com.hnyfkj.jyindustry.common.utils.ShiroUtils;

/**
 * @author zjq 角色表
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
	@Autowired
	private SysRoleDao sysRoleDao;
	@Autowired
	private SysMemberInfoDao sysMemberInfoDao;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int insert(final SysRoleInsertDto sysRoleDto) {
		final SysRoleEntity sysRole = new SysRoleEntity();
		BeanUtils.copyProperties(sysRoleDto, sysRole);
		checkRoleNameExist(sysRole);
		sysRole.setAddMember((String) ShiroUtils.getSessionAttribute("memberId"));
		sysRole.setModifyTime(LocalDateTime.now());
		int result=sysRoleDao.insert(sysRole);
		List<Integer> menu=sysRoleDto.getMenu();
		//添加菜单角色关系
		String memberId=(String)ShiroUtils.getSessionAttribute("memberId");
		sysRoleDao.insertRoleAndMenu(menu, sysRole.getRoleId(),memberId,new Date());
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int update(SysRoleUpdateDto sysRoleDto) {
		final SysRoleEntity sysRole = new SysRoleEntity();
		BeanUtils.copyProperties(sysRoleDto, sysRole);
		checkRoleNameExist(sysRole);
		sysRole.setModifyTime(LocalDateTime.now());
		int result= sysRoleDao.updateById(sysRole);
		//删除原菜单角色关系
		sysRoleDao.delRoleAndMenu(sysRole.getRoleId());
		List<Integer> menu=sysRoleDto.getMenu();
		//添加新菜单角色关系
		String memberId=(String)ShiroUtils.getSessionAttribute("memberId");
		sysRoleDao.insertRoleAndMenu(menu, sysRole.getRoleId(),memberId,new Date());
		return result;
	}

	private void checkRoleNameExist(SysRoleEntity sysRole) {
		QueryWrapper<SysRoleEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("role_name", sysRole.getRoleName());
		List<SysRoleEntity> sysRoleEntities = sysRoleDao.selectList(queryWrapper);
		if (!CollectionUtils.isEmpty(sysRoleEntities)) {
			if (sysRole.getRoleId() != null) {
				for (SysRoleEntity entity : sysRoleEntities) {
					if (entity.getRoleId().equals(sysRole.getRoleId())) {
						continue;
					}
					if (entity.getRoleName() != null && entity.getRoleName().equals(sysRole.getRoleName())) {
						throw new HnyfkjBusinessException("名称重复");
					}
				}
			} else {
				throw new HnyfkjBusinessException("名称重复");
			}
		}
	}

	@Override
	public PageUtils<SysRoleEntity> selectListDto(SysRoleQueryDto queryDto) {
		PageHelper.startPage(queryDto.getPage(), queryDto.getLimit());
		PageInfo<SysRoleEntity> pageInfo = new PageInfo<>(sysRoleDao.selectListDto(queryDto));
		pageInfo.setPageNum(queryDto.getPage());
		return PageUtils.pageOf(pageInfo);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public int delectBatch(Integer[] roleIds) {
		QueryWrapper<SysMemberInfoEntity> queryWrapper = new QueryWrapper<SysMemberInfoEntity>();
		queryWrapper.in("role_type", Arrays.asList(roleIds));
		List<SysMemberInfoEntity> list = this.sysMemberInfoDao.selectList(queryWrapper);
		SysRoleEntity sysRoleEntity = null;

		for (SysMemberInfoEntity entity : list) {
			sysRoleEntity = this.sysRoleDao.selectById(entity.getRoleType());
			throw new HnyfkjBusinessException(sysRoleEntity.getRoleName() + "-在用，无法删除");
		}
		//删除菜单关联
		List<Integer> list2=Arrays.asList(roleIds);
		String roleIdStr=list2.toString().replace("[", "(").replace("]", ")");
		sysRoleDao.deleteRoleId(roleIdStr);
		return sysRoleDao.deleteBatchIds(Arrays.asList(roleIds));
	}

	@Override
	public List<SysRoleEntity> getRoleList() {
		final SysRoleQueryDto queryDto=new SysRoleQueryDto();
		return sysRoleDao.selectListDto(queryDto);
	}

}
